package com.lanou.service.impl;

import com.lanou.bean.DiaryFinishOrder;
import com.lanou.bean.DiaryPhoto;
import com.lanou.bean.User;
import com.lanou.mapper.DiaryMapper;
import com.lanou.service.DiaryService;
import com.lanou.utils.AjaxResult;
import com.lanou.utils.TimeUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/3/16.
 */
@Service
public class DiaryServiceImpl implements DiaryService {
    @Resource
    private DiaryMapper diaryMapper;


    /**
     * 徐鸣 用户创建日记,需要分别把数据插入三张表
     * @param writeDiary 日记内容
     * @param stage     装修阶段
     * @param diaryLabel 日记标签
     * @param date      日期
     * @param diaryPhotos  插入多个日记照片
     * @param finishOrders  插入多个装修订单
     */
    @Override
    public void insertDiary(String writeDiary, String stage, String diaryLabel, String date,
                            DiaryPhoto[] diaryPhotos,DiaryFinishOrder[] finishOrders) {

        diaryMapper.insertDiary(writeDiary, stage, diaryLabel, date);

        // 根据日记时间来查到该日记的主键id
        Integer diaryId = diaryMapper.selectDiaryId(date);

        // 把前端传过来的多个图片数据循环遍历插入日记图片表(dp_diaryPhoto),并且插入日记id
        for (DiaryPhoto diaryPhoto : diaryPhotos) {
            diaryMapper.insertDiaryPhoto(diaryPhoto.getPhotoUrl(),diaryId);
        }
        // 把前端传过来的多个装修订单循环遍历插入装修订单表(dfo_diaryFinishOrder),并且插入日记id
        for (DiaryFinishOrder finishOrder : finishOrders) {
            diaryMapper.insertDiaryFinishOrder(finishOrder.getProductName(), finishOrder.getPrice(),
                    finishOrder.getCount(), finishOrder.getBrand(), finishOrder.getBuyAddress(), finishOrder.getStyle(),
                    finishOrder.getClasses(), finishOrder.getRemark(), diaryId);
        }

    }

    @Override
    public void insertNewDiary(String diaryTitle, String houserType, String area, String city,
                               String style, String decorateWay, String villageName, String companyName) {
        // 用subject获取userId插入日记表
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer userId = user.getU_id();
        diaryMapper.insertNewDiary(userId,diaryTitle,houserType,area,city,style,decorateWay,villageName,companyName);
    }

    /**
     *
     * @param commentContent 评论内容
     * @param diary_id 被评论的日记id
     * @return 插入的结果
     * 开发人:赵帅
     */
    @Override
    public AjaxResult insertDiaryComments(String commentContent, Integer diary_id) {
            User user = (User) SecurityUtils.getSubject().getPrincipal();

            long time =  System.currentTimeMillis();
            String commentTime = TimeUtils.timeTool(time);
            diaryMapper.insertDiaryComments(user.getU_nickname(),user.getU_handpicture(),
                    commentContent,diary_id,commentTime);
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.setCode("200");
            ajaxResult.setMsg("添加评论");
            ajaxResult.setData("添加成功");
            return ajaxResult;

    }

    /**
     *
     * @param repeatToUserNick 回复人的昵称
     * @param commentContent 回复的内容
     * @param commenter_id 回复发出评论人的id
     * @return 回复结果
     * 开发人:赵帅
     */
    @Override
    public AjaxResult insertRepeatContent(String repeatToUserNick, String commentContent, Integer commenter_id) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        diaryMapper.insertRepeatContent(user.getU_nickname(),user.getU_handpicture(),commentContent,repeatToUserNick,commenter_id);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode("200");
        ajaxResult.setMsg("回复评论");
        ajaxResult.setData("回复成功");
        return ajaxResult;
    }

    /**
     * 徐鸣 查询所有业主日记
     * @return
     */
    @Override
    public User selectAllDiary() {
        return diaryMapper.selectAllDiary();
    }


    /**
     * zfy
     * 根据业主日记id 获取该篇日记下的所有装修订单信息
     * @param id
     * @return
     */
    @Override
    public List<DiaryFinishOrder> getDiaryFinishOrderId(Integer id) {
        return diaryMapper.getDiaryFinishOrderId(id);
    }

    /**
     * zfy
     * 获取订单的价格合计
     * @param newDiaryId
     * @return
     */
    @Override
    public double sumPrice(Integer newDiaryId) {
        return diaryMapper.sumPrice(newDiaryId);
    }
}
